import java.util.Scanner;

public class Gomoku {
    private static final int SIZE = 15; // 棋盘大小
    private char[][] board = new char[SIZE][SIZE];

    public Gomoku() {
        // 初始化棋盘
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = '.';
            }
        }
    }

    // 打印棋盘
    public void printBoard() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

    // 检查棋盘是否已满
    public boolean isBoardFull() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (board[i][j] == '.') {
                    return false;
                }
            }
        }
        return true;
    }

    // 检查是否获胜
    public boolean checkWin(int row, int col, char player) {
        // 检查行
        for (int i = 0; i < SIZE; i++) {
            if (board[row][i] != player) {
                break;
            }
            if (i - col + 1 >= 4) {
                return true;
            }
        }

        // 检查列
        for (int i = 0; i < SIZE; i++) {
            if (board[i][col] != player) {
                break;
            }
            if (i - row + 1 >= 4) {
                return true;
            }
        }

        // 检查主对角线
        int dr = 1, dc = 1;
        while (row - dr >= 0 && col - dc >= 0 && board[row - dr][col - dc] == player) {
            if (dr == 4) return true;
            dr++;
            dc++;
        }
        dr = 1;
        dc = 1;
        while (row + dr < SIZE && col + dc < SIZE && board[row + dr][col + dc] == player) {
            if (dr == 4) return true;
            dr++;
            dc++;
        }

        // 检查副对角线
        dr = 1;
        dc = -1;
        while (row - dr >= 0 && col + dc < SIZE && board[row - dr][col + dc] == player) {
            if (dr == 4) return true;
            dr++;
            dc++;
        }
        dr = 1;
        dc = -1;
        while (row + dr < SIZE && col - dc >= 0 && board[row + dr][col - dc] == player) {
            if (dr == 4) return true;
            dr++;
            dc++;
        }

        return false;
    }

    // 玩家下棋
    public boolean placePiece(int row, int col, char player) {
        if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || board[row][col] != '.') {
            return false;
        }
        board[row][col] = player;
        if (checkWin(row, col, player)) {
            System.out.println("Player " + player + " wins!");
            return true;
        }
        if (isBoardFull()) {
            System.out.println("It's a draw!");
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Gomoku game = new Gomoku();
        game.printBoard();
        char currentPlayer = 'X';

        while (true) {
            System.out.println("Player " + currentPlayer + "'s turn. Enter row and column (e.g., 5 5):");
            int row = scanner.nextInt();
            int col = scanner.nextInt();
            if (game.placePiece(row - 1, col - 1, currentPlayer)) {
                break;
            }
            game.printBoard();
            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        }
        scanner.close();
    }
}